package com.kaili.basic.service.impl;/**
 * Created by dxj on 18-5-10.
 */

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.BaseBarcodeRuleMapper;
import com.kaili.basic.mapper.BaseDictMapper;
import com.kaili.basic.pojo.dto.BarcoderuleAddDto;
import com.kaili.basic.pojo.entity.BaseBarcodeRule;
import com.kaili.basic.pojo.entity.BaseDict;
import com.kaili.basic.service.IBaseBarcodeRuleService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author dxj
 * @Copyright: Copyright (c) 2017
 * @Company: 杭州凯立通信有限公司
 */
@Service
public class BaseBarcodeRuleServiceImpl implements IBaseBarcodeRuleService{

    @Autowired
    private BaseBarcodeRuleMapper baseBarcodeRuleMapper;

    @Autowired
    private BaseDictMapper dictMapper;

    //根据id查询条码规则
    public BaseBarcodeRule getBaseBarcodeRuleById(Long id){
        return baseBarcodeRuleMapper.getBaseBarcodeRuleById(id);
    }


    //根据条码规则查询最大件数
    @Override
    public Integer getBaseBarcodeRuleByRule(String rule) {
        return baseBarcodeRuleMapper.getBaseBarcodeRuleByRule(rule);
    }


    //根据parentid查询条码规则
    public BaseBarcodeRule getBaseBarcodeRuleByParentId(Long parentId){
        return baseBarcodeRuleMapper.getBaseBarcodeRuleByParentId(parentId);
    }

    //添加条码规则
    public boolean addNewBaseBarcodeRule(BarcoderuleAddDto barcoderuleAddDto) {
        BaseBarcodeRule baseBarcodeRule = new BaseBarcodeRule();
        BeanUtils.copyProperties(barcoderuleAddDto, baseBarcodeRule);
        BaseBarcodeRule baseBarcodeRule1 = baseBarcodeRuleMapper.loadBarcodeRuleByUsernameExit(baseBarcodeRule);
        if (baseBarcodeRule1 != null){
            return false;
        }
        //长度为空，返回
        if (baseBarcodeRule.getLength() == null){
            return false;
        }

        //子单长度为空，返回
        /*if (baseBarcodeRule.getChLength() == null){
            return false;
        }*/
        baseBarcodeRule.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        baseBarcodeRule.setCreateTime(new Date());
        //根据子单长度输入最大子单件数
        /*if (baseBarcodeRule.getChLength() == 0){
            baseBarcodeRule.setMaxChPiece(0);
        }else if (baseBarcodeRule.getChLength() == 1){
            baseBarcodeRule.setMaxChPiece(9);
        }else if (baseBarcodeRule.getChLength() == 2){
            baseBarcodeRule.setMaxChPiece(99);
        }else if (baseBarcodeRule.getChLength() == 3){
            baseBarcodeRule.setMaxChPiece(999);
        }else if (baseBarcodeRule.getChLength() == 4){
            baseBarcodeRule.setMaxChPiece(9999);
        }*/
        int flag = baseBarcodeRuleMapper.addNewBaseBarcodeRule(baseBarcodeRule);
        if (flag > 0){
            return true;
        }else{
            return false;
        }
        /*if (flag != 0){
            if (baseBarcodeRule.getEwbType().longValue() == 70L){
                if (baseBarcodeRule.getChLength() > 0){
                    //主单，默认添加对应的子单,当子单长度为0时不添加子单规则
                    BaseBarcodeRule zRule = new BaseBarcodeRule();
                    zRule.setRdstatus(1);
                    zRule.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
                    zRule.setCreateTime(new Date());
                    String zRuleString = baseBarcodeRule.getBarcodeRule().substring(0,baseBarcodeRule.getBarcodeRule().length()-3);
                    zRuleString = zRuleString + "[0-9]{"+baseBarcodeRule.getChLength()+"})$/";
                    zRule.setBarcodeRule(zRuleString);
                    BaseDict dict = dictMapper.selectByDictName("子单");
                    if (dict != null){
                        zRule.setEwbType(dict.getDictId().longValue());
                    }else {
                        zRule.setEwbType(71L);
                    }
                    zRule.setLength(baseBarcodeRule.getLength() +baseBarcodeRule.getChLength());
                    zRule.setRemark("创建主单，自动生成子单规则");
                    zRule.setParentId(baseBarcodeRule.getId().longValue());
                    baseBarcodeRuleMapper.addNewBaseBarcodeRule(zRule);
                }


            }
            return true;
        }else{
            return false;
        }*/
    }

    public int updateById(BaseBarcodeRule baseBarcodeRule){
        baseBarcodeRule.setModifyTime(new Date());
        baseBarcodeRule.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        int flag = baseBarcodeRuleMapper.updateById(baseBarcodeRule);
        return flag;
    }

    public int deleteBatchIds(List<Long> ids){
        int flag = baseBarcodeRuleMapper.deleteBatchIds(ids);
        return flag;
    }

    public Page<BaseBarcodeRule> queryPagedList(Page<BaseBarcodeRule> page, BaseBarcodeRule baseBarcodeRule){
        List<BaseBarcodeRule> result = baseBarcodeRuleMapper.queryPagedList(page,baseBarcodeRule);
        return page.setRecords(result);
    }

    @Override
    public boolean selectCount() {
        Integer count=baseBarcodeRuleMapper.selectCount();
        if(count>0){
            return false;
        }
        return true;
    }


    //判断 流水号规则是否存在

    public boolean ExitSerialNum(Long serialNum){
        Integer count=baseBarcodeRuleMapper.ExitSerialNum(serialNum);
        if(count>0){
            return false;
        }
        return true;
    }
}
